package com.lkh.real;

import com.lkh.common.Config;
import com.lkh.sink.HbaseMapper;
import com.lkh.sink.HbaseSink;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.hadoop.hbase.client.Put;

import java.util.Properties;

public class ComputeWeiBoUserToHbase {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        Properties kafkaProps = new Properties();
        kafkaProps.setProperty("zookeeper.connect", Config.getString("kafka.zookeeper.connect"));
        kafkaProps.setProperty("bootstrap.servers", Config.getString("kafka.bootstrap.servers"));
        kafkaProps.setProperty("group.id", "asdasdssad");

        //构建kafkaSource
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>(
                "user",
                new SimpleStringSchema(),
                kafkaProps);

        kafkaConsumer.setStartFromEarliest();


        DataStreamSource<String> dataStreamSource = env.addSource(kafkaConsumer);

        HbaseMapper<String> hbaseMapper = new HbaseMapper<String>() {
            @Override
            public Put mapper(String value) {

                String[] split = value.split("\\|");
                String user_id = split[0];
                String user_name = split[1];
                String gender = split[2];
                String description = split[3];
                String follow_count = split[4];
                String followers_count = split[5];

                Put put = new Put(user_id.getBytes());

                put.add("info".getBytes(), "user_name".getBytes(), user_name.getBytes());
                put.add("info".getBytes(), "gender".getBytes(), gender.getBytes());
                put.add("info".getBytes(), "description".getBytes(), description.getBytes());
                put.add("info".getBytes(), "follow_count".getBytes(), follow_count.getBytes());
                put.add("info".getBytes(), "followers_count".getBytes(), followers_count.getBytes());

                return put;

            }
        };


        HbaseSink<String> hbaseSink = new HbaseSink<>(
                "user",
                hbaseMapper,
                Config.getString("hbase.zookeeper.quorum")
        );

        SingleOutputStreamOperator<String> streamOperator = dataStreamSource.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return value.split("\\|").length == 6;
            }
        });


        //微博用户数据导入hbase
        streamOperator.addSink(hbaseSink);


        env.execute();

    }
}
